﻿using MediatR;
using System;
using System.Threading;
using System.Threading.Tasks;
using wt.wine.Application.Common.Interfaces;
using wt.wine.Application.Common.Models;
using wt.wine.Domain.Entities;

namespace wt.wine.Application.ManageDevicerecordtalbe.Command
{
    /// <summary>
    /// 设备加酒接口
    /// </summary>
    public class AddDevicerecordCommand:IRequest<Json<string>>
    {
        /// <summary>
        /// 设备 id
        /// </summary>
        public int deviceid { get; set; }
        /// <summary>
        /// 加酒量 单位:ml
        /// </summary>
        public int num { get; set; }
        /// <summary>
        /// 现存容量  （加酒后实际现存容量）
        /// </summary>
        public int existingCapacity { get; set; }
        /// <summary>
        /// 备注
        /// </summary>
        public string remark { get; set; } = "";
        /// <summary>
        /// 
        /// </summary>
        public string source { get; set; }
    }

    public class AddDevicerecordCommandHandler : IRequestHandler<AddDevicerecordCommand, Json<string>>
    {
        private readonly ICurrentUserService _icurrentuserservices;
        private readonly IDevicetableRepository _idevicetableRepository;
        private readonly IskutableRepository _iskutableRepository;
        private readonly IDevicerecordRepository _idevicerecordRepository;
        public AddDevicerecordCommandHandler(ICurrentUserService icurrentuserservices, IDevicetableRepository idevicetableRepository, IskutableRepository iskutableRepository, IDevicerecordRepository idevicerecordRepository)
        {
            _icurrentuserservices = icurrentuserservices;
            _idevicetableRepository = idevicetableRepository;
            _iskutableRepository = iskutableRepository;
            _idevicerecordRepository = idevicerecordRepository;
        }
        public async Task<Json<string>> Handle(AddDevicerecordCommand request, CancellationToken cancellationToken)
        {
            int userid = _icurrentuserservices.GetUserId();
            if (userid <= 0)
            {
                return Json<string>.Failed("请先登录", "");
            }
            else if (request.deviceid <= 0)
            {
                return Json<string>.Failed("请选择需要补充酒的设备", "");
            }
            else if (request.num <= 0)
            {
                return Json<string>.Failed("请选择需要注入的酒的数量", "");
            }
            else
            {
                var device = await _idevicetableRepository.GetDevicetableByid(request.deviceid, cancellationToken);
                if (!(device != null && device.id > 0))
                {
                    return Json<string>.Failed("请选择有效的设备", "");
                }
                if (device.binduserid != userid)
                {
                    return Json<string>.Failed("请绑定设备人操作", "");
                }
                else if (device.currentcapacity + request.num > device.maxcapacity)
                {
                    //return Json<string>.Failed("注入的酒量大于设备最大容量", "");
                    Console.WriteLine($"Time：{DateTime.Now:yyyy-MM-dd HH:mm:ss} ,加酒后容量大于总容量，现存容量更新为：{request.existingCapacity}");
                }
                else if (device.skuid <= 0)
                {
                    return Json<string>.Failed("请将设备绑定产品", "");
                }
                var product=await _iskutableRepository.GetSkuTableById(device.skuid, cancellationToken);
                if (!(product != null && product.id > 0))
                {
                    return Json<string>.Failed("请绑定有效的产品", "");
                }
                else if (product.userid != userid)
                {
                    return Json<string>.Failed("请录入产品人操作", "");
                }
                devicerecordtalbe model = new devicerecordtalbe();
                model.deviceid = device.id;
                model.skuid = device.skuid;
                model.capacity = request.num;
                model.createtime = DateTime.Now;
                model.updatetime = DateTime.Now;
                model.userid = userid;
                model.userpid = userid;
                model.name = product.name;
                model.price = product.price;
                model.state = 0;
                model.deviceidno = device.deviceid;
                model.cusdeviceid = device.cusdeviceid;
                model.remark = request.remark;
                model.unit = product.unit;
                await _idevicerecordRepository.AddDevicerecord(model,cancellationToken);
                if(request.existingCapacity > 0 && device.currentcapacity + request.num > device.maxcapacity)
                {
                    device.currentcapacity = request.existingCapacity;
                }
                else
                {
                    device.currentcapacity += request.num;
                }
                await _idevicetableRepository.EditDevicecapacitythreshold(device, cancellationToken);
                return Json<string>.Success("录入成功", "");
            }
        }
    }
}
